﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CMS.BLL;

public partial class Controls_Products_ctrProductDetails : System.Web.UI.UserControl
{
    private int nCurrentLanguage = Support.GetCurrentLanguage();
    private int nDefaultLanguage = Support.GetDefaultLanguage ();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.SetLabel();
            this.InitData();
        }
    }
    private void SetLabel()
    {
        lblQuantity.Text = MultipleLanguage.QUANTITY[nCurrentLanguage - 1];
        lblDescription.Text = MultipleLanguage.PRODUCT_DESCRIPTION[nCurrentLanguage - 1];
        lnkRoot_Product.Text = MultipleLanguage.PRODUCT[nCurrentLanguage - 1];
        lnkRoot_Product.NavigateUrl = Config.ROOT + Pages.FrontEnds.PRODUCT_CATEGORY;
        lblGuine.Text = MultipleLanguage.PRODUCT_GUINE[nCurrentLanguage - 1];
        lblImage.Text = MultipleLanguage.PRODUCT_IMAGE[nCurrentLanguage - 1];
        lblSameCategory.Text = MultipleLanguage.PRODUCT_SAMECAT[nCurrentLanguage - 1];
        lblSpecification.Text = MultipleLanguage.PRODUCT_SPEC[nCurrentLanguage - 1];
        lblTag.Text = MultipleLanguage.PRODUCT_TAG[nCurrentLanguage - 1];
    }
    private void InitData()
    {
        int nID = ConvertData.ConvertToInt(Request.QueryString[Config.PRO_ID]);
        try
        {
            Products obj = new Products();
            obj.LoadById(nID);
            this.BindGallery(obj);
            lblOriginal.Text = MultipleLanguage.LBL_ORIGIN[nCurrentLanguage - 1] + ":" + " " + ConvertData.ConvertToString(obj.Data.Original).ToUpper();
            this.SetSubLink(obj.Data.ProductCategoryID);
            string sImageDefault = obj.Data.ImageDefault;
            string sImage1 = obj.Data.Image1;
            string sImage2 = obj.Data.Image2;
            string sImage3 = obj.Data.Image3;
            string sImage4 = obj.Data.Image4;
            if (sImageDefault == "")
            {
                atag.HRef = Config.IMAGE_NO_IMAGE;
                imgImage1.ImageUrl = Config.IMAGE_NO_IMAGE;
            }

            else
            {
                atag.HRef = Config.ROOT + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD + obj.Data.Image1;
                imgImage1.ImageUrl = Config.ROOT + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD + obj.Data.Image1;
            }

            if (sImage1 == "")
                imgImage2.ImageUrl = Config.IMAGE_NO_IMAGE;
            else imgImage2.ImageUrl = Config.ROOT + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD + obj.Data.Image1;

            if (sImage2 == "")
                imgImage3.ImageUrl = Config.IMAGE_NO_IMAGE;
            else imgImage3.ImageUrl = Config.ROOT + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD + obj.Data.Image2;

            if (sImage3 == "")
                imgImage4.ImageUrl = Config.IMAGE_NO_IMAGE;
            else imgImage4.ImageUrl = Config.ROOT + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD + obj.Data.Image3;

            if (sImage4 == "")
                imgImage5.ImageUrl = Config.IMAGE_NO_IMAGE;
            else imgImage5.ImageUrl = Config.ROOT + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD + obj.Data.Image4;

            lblProductCode.Text = MultipleLanguage.LBL_PRODUCTCODE[nCurrentLanguage - 1] + ":" + " " + obj.Data.ProductCode;
            lblTrademark.Text = MultipleLanguage.LBL_TRADEMARK[nCurrentLanguage - 1] + ":" + " " + GetTrademark(obj.Data.TradeMarkID).ToUpper();

            this.SetAttachedFile(nID);
            this.SetSameCategory(nID);
            string sSpec = "";
            switch (nCurrentLanguage)
            {
                case 1:
                    lblProductName.Text = ConvertData.ConvertToString(obj.Data.ProductName1).ToUpper();
                    sSpec = ConvertData.ConvertToString(obj.Data.Specifications1);
                    ltrContent.Text = ConvertData.ConvertToString(obj.Data.MainContent1);
                    break;
                case 2:
                    lblProductName.Text = ConvertData.ConvertToString(obj.Data.ProductName2).ToUpper();
                    sSpec = ConvertData.ConvertToString(obj.Data.Specifications2);
                    ltrContent.Text = ConvertData.ConvertToString(obj.Data.MainContent2);
                    break;
                case 3:
                    lblProductName.Text = ConvertData.ConvertToString(obj.Data.ProductName3).ToUpper();
                    sSpec = ConvertData.ConvertToString(obj.Data.Specifications3);
                    ltrContent.Text = ConvertData.ConvertToString(obj.Data.MainContent3);
                    break;
            }
            if (sSpec.Length > 0)
            {
                ltrSpec.Text = sSpec;
            }
            else
            {
                ltrSpec.Text = MultipleLanguage.SEARCH_NOITEM[nCurrentLanguage - 1];
            }

            lblSize.Text = ConvertData.ConvertToString(obj.Data.Size);
            lblTagDetail.Text = ConvertData.ConvertToString(obj.Data.Tag);
            double dPrice = ConvertData.ConvertToDouble(obj.Data.Price);
            if (dPrice > 0)
            { lblPrice.Text = MultipleLanguage.PRICE[nCurrentLanguage-1] + Support.FormatCurrency(dPrice); }
            else
            {
                lblPrice.Text = MultipleLanguage.PRICE[nCurrentLanguage-1] + MultipleLanguage.PRICE_CALL[nCurrentLanguage - 1];
            }

            string sColor = ConvertData.ConvertToString(obj.Data.Colors);
            double dSaleOff = ConvertData.ConvertToDouble(obj.Data.PriceSaleOff);
            int isSaleOff = ConvertData.ConvertToInt(obj.Data.IsSalesOff);
            if (isSaleOff > 0)
            {
                lblSaleOff.Visible = true;
                if (dSaleOff > 0)
                {
                    lblSaleOff.Text = MultipleLanguage.PRICE_SALEOFF[nCurrentLanguage-1] + " " + Support.FormatCurrency(obj.Data.PriceSaleOff) + " " + "₫";
                    if (dPrice > 0)
                    {
                        lblPrice.CssClass = "linethrough";
                        lblPrice.Visible = true;
                        lblPrice.Text = Support.FormatCurrency(obj.Data.Price) + " " + "₫";
                    }
                    else
                    {
                        lblPrice.Visible = false;
                    }
                }
                else
                {
                    lblSaleOff.Text = MultipleLanguage.PRICE_SALEOFF[nCurrentLanguage - 1] + MultipleLanguage.PRICE_CALL[nCurrentLanguage - 1];
                }

            }
            else
            { lblSaleOff.Visible = false; }

            if (sColor.Length > 0)
            {
                this.ConvertColors(sColor);
            }
        }
        catch { Response.Redirect("~/Default.aspx"); }
    }
    private void SetSubLink(int nCatID)
    {


        Products objProduct = new Products();
        int nID = ConvertData.ConvertToInt(Request.QueryString[Config.PRO_ID]);
        objProduct.LoadById(nID);
        int nRootCat = objProduct.Data.ProductCategoryID;

        ProductCategory objProductCat = new ProductCategory();
        objProductCat.LoadById(nRootCat);

        switch (nCurrentLanguage)
        {
            case 1:
                lnkSub3_Product.Text = " " + ">" + " " + objProductCat.Data.ProductCategoryName1;
                break;
            case 2:
                lnkSub3_Product.Text = " " + ">" + " " + objProductCat.Data.ProductCategoryName2;
                break;
            case 3:
                lnkSub3_Product.Text = " " + ">" + " " + objProductCat.Data.ProductCategoryName3;
                break;
        }
        lnkSub3_Product.NavigateUrl = Config.ROOT + Pages.FrontEnds.PRODUCT_CATEGORY + "?" + Config.PRO_CAT + "=" + nRootCat;


        int nParent = objProductCat.Data.Parent;

        if (nParent > 0)
        {
            lnkSub2_Product.NavigateUrl = Config.ROOT + Pages.FrontEnds.PRODUCT_CATEGORY + "?" + Config.PRO_CAT + "=" + nParent;
            objProductCat.LoadById(nParent);
            switch (nCurrentLanguage)
            {
                case 1:
                    lnkSub2_Product.Text = " " + ">" + " " + objProductCat.Data.ProductCategoryName1;
                    break;
                case 2:
                    lnkSub2_Product.Text = " " + ">" + " " + objProductCat.Data.ProductCategoryName2;
                    break;
                case 3:
                    lnkSub2_Product.Text = " " + ">" + " " + objProductCat.Data.ProductCategoryName3;
                    break;
            }


            int nSub1Parent = objProductCat.Data.Parent;
            if (nSub1Parent > 0)
            {
                lnkSub1_Product.NavigateUrl = Config.ROOT + Pages.FrontEnds.PRODUCT_CATEGORY + "?" + Config.PRO_CAT + "=" + nSub1Parent;

                objProductCat.LoadById(nSub1Parent);
                int nSub2Parent = objProductCat.Data.Parent;

                switch (nCurrentLanguage)
                {
                    case 1:
                        lnkSub1_Product.Text = " " + ">" + " " + objProductCat.Data.ProductCategoryName1;
                        break;
                    case 2:
                        lnkSub1_Product.Text = " " + ">" + " " + objProductCat.Data.ProductCategoryName2;
                        break;
                    case 3:
                        lnkSub1_Product.Text = " " + ">" + " " + objProductCat.Data.ProductCategoryName3;
                        break;
                }
            }

        }
    }
    private void SetAttachedFile(int nProductID)
    {
        Products objProduct = new Products();
        objProduct.LoadById(nProductID);
        string sLink = ConvertData.ConvertToString(objProduct.Data.UserGuide);
        if (sLink.Length  > 0)
        {
            lnkUserGuide.Text = "Download file: " + sLink;
            lnkUserGuide.NavigateUrl = Config.ROOT + Config.FILE_DEFAULT_UPLOAD + sLink;
        }
        else
        {
            lnkUserGuide.Text  = MultipleLanguage.SEARCH_NOITEM[nCurrentLanguage - 1]; 
        }
      
    }

    private void SetSameCategory(int nProductID)
    {
        Products objProd = new Products();
        objProd.LoadById(nProductID);
        int nCategory = objProd.Data.ProductCategoryID;

        string sParent = "";
        ProductCategory objProductCategory = new ProductCategory();
        DataTable dtbProductCategory = new DataTable();
        dtbProductCategory = objProductCategory.SearchByParent(nCategory);
        int nCount = dtbProductCategory.Rows.Count;
        sParent = ConvertData.ConvertToString(nCategory);

        if (nCount > 0)
        {

            for (int i = 0; i < nCount; i++)
            {
                sParent = sParent + "," + ConvertData.ConvertToString(dtbProductCategory.Rows[i]["ProductCategoryID"]) + ",";

                ProductCategory objProductCategory2 = new ProductCategory();
                DataTable dtbProductCategory2 = new DataTable();
                dtbProductCategory2 = objProductCategory2.SearchByParent(ConvertData.ConvertToInt(dtbProductCategory.Rows[i]["ProductCategoryID"]));
                int nCount2 = dtbProductCategory2.Rows.Count;
                if (nCount2 > 0)
                {

                    for (int y = 0; y < nCount2; y++)
                    {
                        sParent = sParent + "," + ConvertData.ConvertToString(dtbProductCategory2.Rows[y]["ProductCategoryID"]) + ",";

                        ProductCategory objProductCategory3 = new ProductCategory();
                        DataTable dtbProductCategory3 = new DataTable();
                        dtbProductCategory3 = objProductCategory3.SearchByParent(ConvertData.ConvertToInt(dtbProductCategory2.Rows[y]["ProductCategoryID"]));
                        int nCount3 = dtbProductCategory3.Rows.Count;
                        if (nCount3 > 0)
                        {
                            for (int z = 0; z < nCount3; z++)
                            {
                                sParent = sParent + "," + ConvertData.ConvertToString(dtbProductCategory3.Rows[z]["ProductCategoryID"]) + ",";

                                ProductCategory objProductCategory4 = new ProductCategory();
                                DataTable dtbProductCategory4 = new DataTable();
                                dtbProductCategory4 = objProductCategory4.SearchByParent(ConvertData.ConvertToInt(dtbProductCategory3.Rows[z]["ProductCategoryID"]));
                                int nCount4 = dtbProductCategory4.Rows.Count;
                                if (nCount4 > 0)
                                {
                                    for (int z1 = 0; z1 < nCount4; z1++)
                                    {
                                        sParent = sParent + "," + ConvertData.ConvertToString(dtbProductCategory4.Rows[z1]["ProductCategoryID"]) + ",";
                                    }
                                }
                            }
                        }
                    }
                }
            }
            sParent = sParent.Replace(",,", ",");
            sParent = ConvertData.TruncateString(sParent, sParent.Length - 1);
        }

        int nTrademark = 0;
        int nMaterial = 0;
        double dPriceFrom = 0;
        double dPriceTo = 100000000;
        string sProductName = "";
        int nStatus = 1;
        int nPageCount = 0;
        int nItemView = 6;
        int isSaleOff = -1;
        int nPageIndex = 1;
        int nPageSize = 6;    
        Products objProduct = new Products();
        DataTable dtbProduct = new DataTable();
        dtbProduct = objProduct.Search(nItemView, "", sProductName, nProductID, sParent, nTrademark, nMaterial, dPriceFrom, dPriceTo, isSaleOff, -1, nStatus, 0, 0, 0, nDefaultLanguage, nCurrentLanguage, nPageIndex, nPageSize, ref nPageCount);
        rptProduct.DataSource = dtbProduct;
        rptProduct.DataBind();
    }
    protected void rptProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        string sCode = string.Empty;
        string sContact = string.Empty;
        string sPrice = string.Empty;
        int nCurrentLang = Support.GetCurrentLanguage();

        sPrice = MultipleLanguage.PRICE[nCurrentLang - 1] + ": ";
        sContact = MultipleLanguage.PRICE_CALL[nCurrentLang - 1] + ": ";
        sCode = MultipleLanguage.PROCODE[nCurrentLang - 1];

        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            ImageButton btnProductImage = (ImageButton)e.Item.FindControl("btnProductImage");
            HyperLink lnkProductName = (HyperLink)e.Item.FindControl("lnkProductName");
            Label lblCode = (Label)e.Item.FindControl("lblCode");
            Label lblPrice = (Label)e.Item.FindControl("lblPrice");
            LinkButton lnkAddCart = (LinkButton)e.Item.FindControl("lnkAddCart");

            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "ProductID"));
            string sProductName = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductName"));
            if (sProductName.Length > 0)
            {
                lnkProductName.Text = sProductName;
            }
            else
            {
                lnkProductName.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductName_Default"));
            }
            lblCode.Text = sCode + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCode"));

            double dPrice = ConvertData.ConvertToDouble(DataBinder.Eval(e.Item.DataItem, "Price"));
            if (dPrice > 0)
            { lblPrice.Text = sPrice + ConvertData.ConvertToString(Support.FormatCurrency(DataBinder.Eval(e.Item.DataItem, "Price"))) + " " + "₫"; }
            else { lblPrice.Text = sPrice + sContact; }

            lnkProductName.NavigateUrl = Config.ROOT + Pages.FrontEnds.PRODUCT_DETAIL + "?" + Config.PRO_ID + "=" + nID;
            lnkAddCart.Text = MultipleLanguage.BUY[nCurrentLang - 1];
            lnkAddCart.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.ADD_BASKET_SUCCCESS[nCurrentLang - 1]);
            btnProductImage.ImageUrl = Config.ROOT + "Images/Upload/Products/" + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ImageDefault"));
            btnProductImage.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductID"));
            lnkAddCart.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductID"));
        }
    }
    protected void rptProduct_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);

        switch (strCommand)
        {
            case "image":
                string sDetail = Config.ROOT + Pages.FrontEnds.PRODUCT_DETAIL + "?" + Config.PRO_ID + "=" + nID;
                Response.Redirect(sDetail);
                break;
            case "addcart":
                // Add product to Cart
                clsBasket objBasket = clsBasket.getclsBaskets(Context);
                ItemclsBaskets item = new ItemclsBaskets(nID);
                objBasket.AddItem(item);
                int nCat = ConvertData.ConvertToInt(Request.QueryString[Config.PRO_CAT]);
                if (nCat > 0)
                { Response.Redirect(this.GetCurrentPageName() + "?" + Config.PRO_CAT + "=" + nCat); }
                else
                {
                    Response.Redirect(this.GetCurrentPageName());
                }
                break;
        }
    }

    private void BindGallery(Products objProduct)
    {
        DataTable myDataTable = new DataTable();
        DataColumn myDataColumn;

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "Image";
        myDataTable.Columns.Add(myDataColumn);

        DataRow row;
        if (objProduct.Data.Image1 != "")
        {
            row = myDataTable.NewRow();
            row["Image"] = objProduct.Data.Image1;
            myDataTable.Rows.Add(row);
        }
        if (objProduct.Data.Image2 != "")
        {
            row = myDataTable.NewRow();
            row["Image"] = objProduct.Data.Image2;
            myDataTable.Rows.Add(row);
        }
        if (objProduct.Data.Image3 != "")
        {
            row = myDataTable.NewRow();
            row["Image"] = objProduct.Data.Image3;
            myDataTable.Rows.Add(row);
        }
        if (objProduct.Data.Image4 != "")
        {
            row = myDataTable.NewRow();
            row["Image"] = objProduct.Data.Image4;
            myDataTable.Rows.Add(row);
        }

        dtlGallery.DataSource = myDataTable;
        dtlGallery.DataBind();
    }
    protected void dtlGallery_ItemDataBound(object sender, DataListItemEventArgs e)
    {

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            System.Web.UI.HtmlControls.HtmlImage imgGallery = (System.Web.UI.HtmlControls.HtmlImage)e.Item.FindControl("imgGallery");
            string sFileName = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Image"));
          
            imgGallery.Src = Config.ROOT + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD  + sFileName;
        }
    }
    
    private void ConvertColors(string sColor)
    {
        if (sColor.Length > 0)
        {
            string sColorDetail = "";
            int nCurrentLang = Support.GetCurrentLanguage();

            string[] sColorID = sColor.Split(',');
            int nTotalColor = sColorID.Length;

            for (int y = 1; y < nTotalColor; y++)
            {
                ProductColor objColor = new ProductColor();
                objColor.LoadById(ConvertData.ConvertToInt(sColorID[y]));
                switch (nCurrentLang)
                {
                    case 1:
                        sColorDetail = sColorDetail + objColor.Data.ColorName1 + ",";
                        break;
                    case 2:
                        sColorDetail = sColorDetail + objColor.Data.ColorName2 + ",";
                        break;
                }
            }
            if (sColorDetail.Length > 0)
                sColorDetail = ConvertData.TruncateString(sColorDetail, sColorDetail.Length - 1);
               lblColors.Text = sColorDetail;
        }
    }

    private string GetTrademark(int nID)
    {
        ProductTradeMark obj = new ProductTradeMark();
        obj.LoadById(nID);
        string sReturn = obj.Data.TradeMarkName;
        return sReturn;
    }
    private string GetCategory(int nID)
    {
        ProductCategory obj = new ProductCategory();
        obj.LoadById(nID);
        string sReturn = string.Empty;
        switch (nCurrentLanguage)
        { 
            case 1:
                sReturn = obj.Data.ProductCategoryName1;
                break;
            case 2:
                sReturn = obj.Data.ProductCategoryName2;
                break;
            case 3:
                sReturn = obj.Data.ProductCategoryName3;
                break;
        }
        return sReturn;
    }
    protected void btnAddToCart_Click(object sender, ImageClickEventArgs e)
    {
        // Add product to Cart
        int nID = ConvertData.ConvertToInt(Request.QueryString["pid"]);
        clsBasket objBasket = clsBasket.getclsBaskets(Context);
        ItemclsBaskets  item = new ItemclsBaskets (nID);
        objBasket.AddItem(item);
       
        Response.Redirect(this.GetCurrentPageName() + "?pid=" + nID);
    }
    protected void btnBuy_Click(object sender, EventArgs e)
    {
        // Add product to Cart
        int nID = ConvertData.ConvertToInt(Request.QueryString[Config.PRO_ID ]);
        clsBasket objBasket = clsBasket.getclsBaskets(Context);
        ItemclsBaskets item = new ItemclsBaskets(nID);
        objBasket.AddItem(item);

        Response.Redirect(this.GetCurrentPageName() + "?" + Config.PRO_ID  + "=" + nID);
    }
    public string GetCurrentPageName()
    {
        string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
        System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
        string sRet = oInfo.Name;
        return sRet;
    }
}